﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;

namespace MetMax.Importer
{
    class LibrarySearch : Base
    {
        #region constructors
        public LibrarySearch(MetMaxDataSet.result_LibrarySearchDataTable dt, string[] filenames, Options opt)
            : base(dt != null ? dt : new MetMaxDataSet.result_LibrarySearchDataTable(), filenames, opt)
        {
        }

        public LibrarySearch(BackgroundWorkerArgument bgwa, Options opt)
            : this(bgwa.dt as MetMaxDataSet.result_LibrarySearchDataTable, bgwa.filenames, opt)
        {
        }
        #endregion constructors

        public new MetMaxDataSet.result_LibrarySearchDataTable dt
        {
            get { return base.dt as MetMaxDataSet.result_LibrarySearchDataTable; }
        }

        public override void Import(string filename, DataColumn currentfileColumn)
        {
            IEnumerable<BusinessObject.Base> resultListCopy = LoadFile(filename).ToList().AsParallel();

            Fitting.Polynomial riFitting = getPolynomialFittingDegree3Rt1ToAlkaneIndex(resultListCopy);

            foreach (BusinessObject.Base v in resultListCopy)
            {
                udtMassSpectra normalisedSpectrum = new udtMassSpectra(v.spectrum).normalizeSpectrumPriorComparison();
                float ri = Convert.ToSingle(riFitting.Interpolate(v.rt1.Value));

                //this is half the Window, one half to the left and one half to the right
                float riWindowRadius = ((ri * 1f) / 100f) / 2f;

                IEnumerable<gmdTools.Matching.PutativLibraryHit> bestRiMatches = from p in gmdTools.GmdSpectrum.GetMatchingSpectra(
                                                                            ri - riWindowRadius
                                                                            , ri + riWindowRadius
                                                                            , RI_UserDefinedFunctions.GcColumnType.VAR5
                                                                            , RI_UserDefinedFunctions.GcMarkerType.ALK
                                                                            , 1
                                                                            , Properties.Settings.Default.GmdConnectionString).AsParallel()
                                                                        orderby p.riDiscrepancy ascending
                                                                        select p;

                throw new NotImplementedException("review this code!!!");
                //IEnumerable<gmdTools.Matching.AnnotatedMatch> matches = (from p in bestRiMatches.AsParallel()
                //                                                         let am = new gmdTools.Matching.AnnotatedMatch(normalisedSpectrum, p, Properties.Settings.Default.GmdConnectionString)
                //                                               where am.DotproductDistance <= 0.5f
                //                                               orderby am.DotproductDistance ascending
                //                                               select am).ToList().AsParallel();

                //if (matches.Count() > 0)
                //{
                //    gmdTools.Matching.AnnotatedMatch m = matches.First();
                //    DataRow tmpRow = dt.FindByspectrum(m.spectrumName);
                //    if (tmpRow == null)
                //    {
                //        tmpRow = dt.Addresult_LibrarySearchRow(m.spectrumName, m.analyteName);
                //    }
                //    tmpRow[currentfileColumn] = new BusinessObject.LibrarySearch(v, matches);
                //}
            }
        }
    }
}
